<?php
class Mysql
{
	public static $db = null;
	public static $dblink = null;
	public static $curlink = null;
	private function __construct($server)
	{
		self::_connect($server);
	}
	
	public static function getInstance($server)
	{
		if (empty(self::$db[$server]) || !(self::$db[$server] instanceof self)){
			$db[$server] = new self($server);
		}
		return $db[$server];
	}
	
	public static function _connect($server)
	{
		if (!isset(self::$dblink[$server]) || empty(self::$dblink[$server])){
			self::$dblink[$server] = self::$curlink = mysql_connect(
				Config::$db[$server]['host'] . ':' . Config::$db[$server]['port'],
				Config::$db[$server]['uname'],
				Config::$db[$server]['passwd']
			);
		}
		mysql_set_charset(CHARSET, self::$dblink[$server]);
		mysql_select_db(Config::$db[$server]['dbname'], self::$dblink[$server]);
	}
	
	public static function query($sql)
	{
		return mysql_query($sql, self::$curlink);
	}
	
	public static function getLastInsertId()
	{
		return mysql_insert_id(self::$curlink);
	}
}